package cn.yoyo.controller.adm;

import cn.yoyo.entity.Book;
import cn.yoyo.tools.RespJson;
import cn.yoyo.service.BookService;
import cn.yoyo.service.LargeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class BookController {
    @Autowired
    BookService bookService;
    @Autowired
    LargeFileService largeFileService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @RequestMapping("/api/adm/book/delete/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        bookService.delete(id);
        // 删除后刷新前端页面
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(new HashMap<>()));
        map.put("books", bookService.findAll());
        return RespJson.pack(200, "删除成功", map);
    }

    @RequestMapping("/api/adm/book/findAll")
    public Map<String, Object> findAll() {
        return RespJson.pack(200, "it's ok...", bookService.findAll());
    }


    @RequestMapping("/api/adm/book/findByPage/{start}/{num}")
    public Map<String, Object> findByPage(@PathVariable("start") Integer start, @PathVariable("num") Integer num) {

        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(new HashMap<>()));
        map.put("books", bookService.findByPage(start, num));
        return RespJson.pack(200, "...", map);
    }

    @RequestMapping("/api/adm/public/getTotalPage")
    public Map<String, Object> getTotalPage() {
        return RespJson.pack(200, "...", bookService.totalPage(new HashMap<>()));
    }

    @RequestMapping("/api/adm/book/findById/{bookid}")
    public Map<String, Object> findById(@PathVariable("bookid") Integer bookid) {
        return RespJson.pack(200, "it's ok...", bookService.findById(bookid));
    }

    @RequestMapping("/api/adm/book/addBook")    // required = false ====> 解决修改书籍时不对图片进行操作的报错问题
    public Map<String, Object> add(Book book, @RequestParam(value = "img", required = false) MultipartFile file) {
        System.out.println(book);
        if (book.getId() == 0) { // book.getId = 0 ==》 添加书籍
            if (file == null || file.getSize() <= 0) {
                return RespJson.pack(404, "文件为空,添加失败", null);
            }
            if (bookService.add(book, file) > 0) {
                // 添加成功之后,把总行数和所有的书本信息返回给前端
                // 刷新按钮的个数 和 新的书本信息
                Map<String, Object> map = new HashMap<>();
                map.put("total", bookService.totalPage(new HashMap<>()));
                map.put("books", bookService.findAll());
                return RespJson.pack(200, "添加成功", map);
            }
        }
        if (book.getId() > 0) { // book.getId > 0  修改书籍信息
            if (bookService.update(book, file) > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("total", bookService.totalPage(new HashMap<>()));
                map.put("books", bookService.findAll());
                return RespJson.pack(200, "修改成功", map);
            }
        }
        return RespJson.pack(200, "", null);
    }
}
